package com.api.syh56.shengyunhui.controller;

import com.api.syh56.shengyunhui.common.*;
import com.api.syh56.shengyunhui.entity.BankCard;
import com.api.syh56.shengyunhui.entity.User;
import com.api.syh56.shengyunhui.entity.WithDraw;
import com.api.syh56.shengyunhui.entity.dto.WithDrawDTO;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.service.WithDrawService;
import com.api.syh56.shengyunhui.websocket.ChatServer;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.ClientInfoStatus;
import java.util.List;


/**
 * 管理端审核提现
 */
@RestController
@RequestMapping("withdraw")
public class WithDrawController {
    @Autowired
    private WithDrawService withDrawService;

    @Autowired
    private ChatServer chatServer;
    /**
     * 钱包提现
     */
    @RequestMapping(value = "/userwithdraw", method = RequestMethod.POST)
    public CommonResult<WithDraw> withdraw(@RequestBody WithDrawDTO withDrawDTO) {
//        if (withDrawDTO.getInvoicePath() == null) {
//            throw new BaseException("请上传发票图片");
//        }
        WithDraw withDraw = new WithDraw();
        BeanUtils.copyProperties(withDrawDTO, withDraw);
        System.out.println(withDraw);
        withDrawService.UserWithdraw(withDraw);
        chatServer.onMessage(Constants.webSocket.WITHDRAW_NEW.toString());
        return CommonResult.success(withDraw);
    }

    /**
     * 提现记录
     */
    @GetMapping(value = "/withdrawrecord")
    public CommonResult<CommonPage<WithDraw>> withdrawRecord(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        Page<WithDraw> page = new Page<>(pageNum,pageSize);
        //查询提现申请中的金额
        QueryWrapper<WithDraw> withDrawQueryWrapper = new QueryWrapper<>();
        withDrawQueryWrapper.lambda()
                .eq(WithDraw::getUid, uid)
                .orderByDesc(WithDraw::getCreationTime);
        Page<WithDraw> withDrawPage = withDrawService.page(page, withDrawQueryWrapper);

        return CommonResult.success(CommonPage.restPage(withDrawPage));
    }

    /**
     * 用户已提现金额
     */
    @GetMapping(value = "/AlreadyWithdraw")
    public CommonResult<BigDecimal> AlreadyWithdraw() {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        QueryWrapper<WithDraw> withDrawQueryWrapper = new QueryWrapper<>();
        withDrawQueryWrapper.lambda()
                .eq(WithDraw::getUid, uid)
                .eq(WithDraw::getState, Constants.WithdrawalStatus.SUCCESS);
        List<WithDraw> list = withDrawService.list(withDrawQueryWrapper);
        BigDecimal TotalWihtdraw = new BigDecimal(0);
        for (WithDraw withDraw : list) {
            TotalWihtdraw = TotalWihtdraw.add(withDraw.getAmount());
        }
        return CommonResult.success(TotalWihtdraw);
    }

    /**
     * 提现申请中金额
     */
    @GetMapping(value = "/WithdrawInProcess")
    public CommonResult<BigDecimal> WithdrawInProcess() {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        QueryWrapper<WithDraw> withDrawQueryWrapper = new QueryWrapper<>();
        withDrawQueryWrapper.lambda()
                .eq(WithDraw::getUid, uid)
                .eq(WithDraw::getState, Constants.WithdrawalStatus.IN_PROGRESS);
        List<WithDraw> list = withDrawService.list(withDrawQueryWrapper);
        BigDecimal TotalWihtdraw = new BigDecimal(0);
        for (WithDraw withDraw : list) {
            TotalWihtdraw = TotalWihtdraw.add(withDraw.getAmount());
        }
        return CommonResult.success(TotalWihtdraw);
    }

}
